<?php

namespace App\Api\Controllers;

use App\Api\Bases\BaseController;
use App\Api\Models\ShippingAddress;
use App\Api\Services\JsonService;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class ShippingAddressController extends BaseController
{
    /**
     * 添加收货地址
     * @param Request $request
     * @return array
     */
    public function addAddress(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
                'name' => 'required|chs|max:10',
                'phone' => 'required|mobile',
                'city' => 'required|chs',
                'area' => 'required|chs',
                'address' => 'chsDash|max:80',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $model = ShippingAddress::saveData($params);
        if (!$model) return JsonService::fail('收货地址添加失败');
        return JsonService::success('收货地址添加成功',$params);
    }



    /**
     * 用户收货地址列表
     * @param Request $request
     * @return array
     */
    public function addressLists(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id' => 'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $lists = ShippingAddress::getUserList($params['member_id']);
        if (!$lists) return JsonService::fail('您还没有添加收货地址');
        return JsonService::success('收货地址添加成功',$lists);
    }

    /**
     * 收货地址详情
     * @param Request $request
     * @return array
     */
    public function addressInfo(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'id'=>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $address = ShippingAddress::addressInfo($params);
        if (!$address) return JsonService::fail('收货地址查询失败');
        return JsonService::success('收货地址查询成功',$address);
    }

    /**
     * 收货地址修改
     * @param Request $request
     * @return array
     */
    public function saveAddress(Request $request)
    {
        if (!$request->isMethod('post')) return JsonService::fail('请求方式不允许');
        try {
            $params = $this->validate($request, [
                'id'=>'required|integer',
                'member_id' => 'required|integer',
                'name' => 'required|chs|max:10',
                'phone' => 'required|mobile',
                'city' => 'required|chs',
                'area' => 'required|chs',
                'address' => 'chsDash|max:80',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $model = ShippingAddress::saveData($params,['id'=>$params['id']]);
        if (!$model) return JsonService::fail('收货地址修改失败');
        return JsonService::success('收货地址修改成功',$params);
    }

    /**
     * 收货地址删除
     * @param Request $request
     * @return array
     */
    public function delAddress(Request $request){
        try {
            $params = $this->validate($request, [
                'id'=>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }

        $model = ShippingAddress::delAddress($params);
        if (!$model) return JsonService::fail('收货地址删除失败');
        return JsonService::success('收货地址删除成功',$params);
    }

    /**
     * 默认收货地址详情
     * @param Request $request
     * @return array
     */
    public function defaultAddress(Request $request)
    {
        try {
            $params = $this->validate($request, [
                'member_id'=>'required|integer',
            ]);
        } catch (ValidationException $validationException) {
            $errors = $validationException->errors();
            return JsonService::fail(array_shift($errors)[0]);
        }
        $where = [
            'member_id'=>$params['member_id'],
            'status' => 1
        ];
        $address = ShippingAddress::addressInfo($where);
        if (!$address) return JsonService::fail('收货地址查询失败');
        return JsonService::success('收货地址查询成功',$address);
    }
}
